package com.ruoyi.asset.park.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.asset.park.domain.bo.ParkMaintenanceSearchBo;
import com.ruoyi.asset.park.domain.bo.StatisticianParkByRegion;
import com.ruoyi.asset.park.domain.vo.ParkCommunityCountVo;
import com.ruoyi.asset.park.domain.vo.ParkCommunityMapVo;
import com.ruoyi.asset.park.domain.vo.ParkMaintenanceVo;
import com.ruoyi.asset.park.domain.vo.ParkWorkStatisticsVo;
import com.ruoyi.asset.park.service.IParkMaintenanceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.wx.SendWxCpMsgUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 公园管养任务完成进度
 *
 * @author lq
 * @date 2023-11-30
 */
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/park")
public class ParkMaintenanceController extends BaseController {

    private final IParkMaintenanceService parkMaintenanceService;

    /**
     * 获取公园管养-管理范围导入模板
     */
    @SaIgnore()
    @Log(title = "公园管养管养范围导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping(value = "/importData")
    public R<List<String>> importData(@RequestParam(name = "excelUrl") String excelUrl) {
        List<String> list = this.parkMaintenanceService.importExcel(excelUrl);

        if (list == null) {
            return R.fail();
        } else {
            return R.ok(list);
        }
    }

    /**
     * 获取公园管养-管理范围导入模板
     */
    @SaIgnore()
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "公园管养管理范围导入模板", ParkMaintenanceVo.class, response);
    }

    /**
     * 删除公园管养管养范围
     */
    @SaIgnore()
    @DeleteMapping("/deletePark/{ids}")
    public R<Void> deleteMaintenanceScope(@NotEmpty(message = "主键不能为空")
                                          @PathVariable Set<Long> ids) {
        int count = this.parkMaintenanceService.deleteMaintenanceScope(ids);

        return toAjax(count >= 0 ? 1 : count);
    }

    /**
     * 查询公园管养管养范围
     */
    @SaIgnore()
    @GetMapping("/queryPark")
    public TableDataInfo<ParkMaintenanceVo> queryMaintenanceScope(ParkMaintenanceSearchBo searchBo, PageQuery pageQuery) {
//        try {
//            String str = SendWxCpMsgUtil.sendTextMsgByUserPhoneNum(1000002, "18672958895", "您正在查询公园管养范围");
//            log.info(str);
//        }catch(WxErrorException ex) {
//            ex.printStackTrace();
//        }
        return this.parkMaintenanceService.queryMaintenanceScope(searchBo, pageQuery);
    }

    /**
     * 导出公园管养管养范围
     */
    @SaIgnore()
    @Log(title = "公园管养管养范围导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPark")
    public void exportMaintenanceScope(ParkMaintenanceSearchBo searchBo, HttpServletResponse response) {
        List<ParkMaintenanceVo> list = this.parkMaintenanceService.queryMaintenanceScope(searchBo);

        ExcelUtil.exportExcel(list, "公园管养管养范围", ParkMaintenanceVo.class, response);
    }

    /**
     * 按区统计公园
     */
    @SaIgnore()
    @GetMapping("/statForParkByRegion")
    public R<List<StatisticianParkByRegion>> statForGreeneryByRegion() {
        List<StatisticianParkByRegion> stat = this.parkMaintenanceService.statForParkByRegion();
        return R.ok(stat);
    }

    @SaIgnore()
    @GetMapping("/parkCommunityCount")
    public R<Map> parkCommunityCount() {
        Map map = this.parkMaintenanceService.getParkCommunityCount();
        return R.ok(map);
    }

    /**
     * 查询智慧大屏公园管养玉塘街道全社区
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/parkCommunity")
    public R<List<ParkCommunityCountVo>> parkCommunity(String date) {
        date = "2024-03-13";
        List<ParkCommunityCountVo> parkCountByCommunity = parkMaintenanceService.getParkCountByCommunity(date);
        return R.ok(parkCountByCommunity);
    }

    /**
     * 查询智慧大屏公园管养按社区区分
     *
     * @param community
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/communityByDate")
    public R<ParkCommunityMapVo> communityByDate(String community, String date) {
        date = "2024-03-13";
        ParkCommunityMapVo communityByDate = parkMaintenanceService.getCommunityByDate(community, date);
        return R.ok(communityByDate);
    }

    /**
     * 查询智慧大屏公园管养今日作业统计与百分比
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/parkWorkByDate")
    public R<ParkWorkStatisticsVo> parkWorkByDate(String date) {
        date = "2024-03-13";
        ParkWorkStatisticsVo parkWorkByDate = parkMaintenanceService.getParkWorkByDate(date);
        return R.ok(parkWorkByDate);
    }

    /**
     * 查询智慧大屏公园管养今日作业统计与百分比
     *
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/parkWorkByDateAndCommunity")
    public R<ParkWorkStatisticsVo> parkWorkByDateAndCommunity(String date, String community) {
        date = "2024-03-13";
        ParkWorkStatisticsVo parkWorkByDate = parkMaintenanceService.getParkWorkByDateAndCommunity(date,community);
        return R.ok(parkWorkByDate);
    }
}
